/*------------------------------------------------------
*店铺网址：https://shop321633213.taobao.com
*作者：一叶孤沙
*日期：2020-11-24
*实验现象：车内有害气体检测
*QQ群：906015840
-------------------------------------------------------*/

#include "mid_co2.h"
#include "usart.h"

static uint8_t co2RxValue;
data_frame_t mid_co2_data = { 0x00 };

void mid_co2_init(void)
{
    drv_usart_init(1);  // USART2
    if (0 == drv_usart_recv_bytes_IT(1, &co2RxValue, 1))
    {
        rt_kprintf("enable uart2 ISR\r\n");
    }
}

rt_err_t mid_co2_check_sum(uint8_t *data, int len)
{
	int data_len = data[1];
	int i = 0, sum = 0;
	
	for (i = 0; i < data_len + 1; i ++ )
	{
		sum += data[i];
	}
	sum = 256 - sum%256;
	
	if ((uint8_t)sum == data[len +2])
	{
		return RT_EOK;
	}
	return RT_ERROR;
}

uint16_t mid_co2_get(void)
{
    uint8_t i = 0;
    uint8_t send_data[4] = {0x11, 0x01, 0x01, 0xED};
		uint8_t recv_data[12] = { 0x00 };

		// get CO2 value
    if (RT_EOK == mid_co2_cmd(send_data, 4, recv_data, 500))
		{
			if (mid_co2_check_sum(mid_co2_data.uart_recv_buff, mid_co2_data.count))
			{
				rt_kprintf("get co2 :");  // 16 5 1 7 d0 3 78 92
				for (i = 0; i < mid_co2_data.count; i++)
				{
					rt_kprintf(" %x", mid_co2_data.uart_recv_buff[i]);
				}
				rt_kprintf("\r\n");
				return mid_co2_data.uart_recv_buff[3] * 256 + mid_co2_data.uart_recv_buff[4];
			}
			else
			{
				rt_kprintf("get co2 error!\r\n");
			}
		}
		
    return 0;
}
 
/*
 * 函数名：ESP8266_Cmd
 * 描述  ：对WF-ESP8266模块发送AT指令
 * 输入  ：cmd，待发送的指令
*         reply1，reply2，期待的响应，为NULL表示不需响应，两者为或逻辑关系
 *         waittime，等待响应的时间
 * 返回  : 1，指令发送成功
 *         0，指令发送失败
 * 调用  ：被外部调用
 */
int mid_co2_cmd ( uint8_t * cmd, uint8_t cmd_len, uint8_t * data_out, uint32_t waittime )
{
//    rt_memset((char *)&mid_co2_data, 0x00, sizeof(mid_co2_data));

    drv_usart_send_bytes(1, cmd, cmd_len);

    while (waittime)
    {
        if (1 == mid_co2_data.full_packet)
        {
            break;
        }
        rt_thread_mdelay(1);
        waittime--;
    }

    if (0 != waittime)
    {
//        rt_memcpy(data_out, mid_co2_data.uart_recv_buff, mid_co2_data.count);
        return RT_EOK;
    }else
    {
        rt_kprintf("get CO2 CMD Error!\r\n");
        return RT_ETIMEOUT;
    }
}

/**
  * @brief  This function handles USART IDLE interrupt.
  */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
    uint8_t len = 0;
    if(UartHandle->Instance == USART2)
    {
        if (DATA_FRAME_START == co2RxValue)  // get 0x16
        {
            mid_co2_data.uart_recv_buff[0] = DATA_FRAME_START;
            //get len
            drv_usart_recv_bytes(1, &co2RxValue, 1);
            len = co2RxValue;
            mid_co2_data.uart_recv_buff[1] = len;
            mid_co2_data.count = len + 3;
            // get rest of data
            drv_usart_recv_bytes(1, &mid_co2_data.uart_recv_buff[2], len);
            // get checksum
            drv_usart_recv_bytes(1, &co2RxValue, 1);
            mid_co2_data.uart_recv_buff[len + 2] = co2RxValue;
            mid_co2_data.full_packet = 1;
//            // 判断是否需要将数据抛到应用层
//            sys_info.recv_bytes_cb(mid_co2_data.uart_recv_buff, mid_co2_data.count);
        }

        drv_usart_recv_bytes_IT(1, &co2RxValue, 1);
    }else if(UartHandle->Instance == USART3)
    {
       mid_cat1_process_uart_recv_msg();
    }
}
